package com.mytijian.runtime;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taobao.api.internal.spi.CheckResult;
import com.taobao.api.internal.spi.SpiUtils;

public class DataSignUtil {

	static Logger log = LoggerFactory.getLogger(DataSignUtil.class);

	public static DataCheckResult checkCodeMerchantSign(HttpServletRequest request, String secret) {
		DataCheckResult result = new DataCheckResult();
		try {
			// 请求参数验签
			CheckResult checkResult = SpiUtils.checkCodeMerchantSign(request, secret);
			result.setRequestBody(checkResult.getRequestBody());
			result.setSuccess(checkResult.isSuccess());

		} catch (Exception ex) {
			log.error("check data sign error: ", ex);
		}
		return result;
	}
	
	public static DataCheckResult checkSign(HttpServletRequest request, String secret) {
		DataCheckResult result = new DataCheckResult();
		try {
			// 请求参数验签
			CheckResult checkResult = SpiUtils.checkSign(request, secret);
			result.setRequestBody(checkResult.getRequestBody());
			result.setSuccess(checkResult.isSuccess());
			
		} catch (Exception ex) {
			log.error("check data sign error: ", ex);
		}
		return result;
	}

	static class DataCheckResult {
		private boolean success;
		private String requestBody;

		public boolean isSuccess() {
			return this.success;
		}

		public void setSuccess(boolean success) {
			this.success = success;
		}

		public String getRequestBody() {
			return this.requestBody;
		}

		public void setRequestBody(String requestBody) {
			this.requestBody = requestBody;
		}
	}
}
